#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence 
@file: court_searchlist_zhixing_worker.py
@time: 2016/12/7 16:00
"""
import time

from common import util
from task.court.court_searchlist_zhixing_worker import CourtSearchListZhiXingWorker


class CourtDetailZhiXingWorker(CourtSearchListZhiXingWorker):
    def __init__(self, **kwargs):
        CourtSearchListZhiXingWorker.__init__(self, **kwargs)

    # 根据ID信息获得页面详情
    def save_keyid_detail(self, session, keyid):

        data = self.get_keyid_detail(session, keyid)
        if data is None:
            return 0

        if data == -1:
            return -1

        # 存入数据
        self.target_db.insert_batch_data(self.target_table, [data], insert=True)
        return 1

    def get_keyid_detail(self, session, keyid):
        data = None
        for _ in xrange(3):
            try:
                # 破解验证码
                captcha_no, result_file = self.get_captcha(session, cap_type=3, proxy_type=2)
                if captcha_no is None:
                    self.log.error('验证码服务出现异常...')
                    time.sleep(1)
                    session.proxies = self.get_proxy(proxy_type=2)
                    continue

                url = self.detail_url + "?id={keyid}&j_captcha={captcha}".format(
                    keyid=keyid, captcha=captcha_no)

                # 请求数据
                r = self.task_request(session.get, url, proxy_type=2, timeout=60)
                if r is None:
                    continue

                if r.text.find('验证码错误') != -1:
                    continue

                if r.text == '':
                    return -1

                if len(r.text) < 10:
                    # self.log.warn('通过keyid获得的数据是空数据..keyid = {keyid}'.format(keyid=keyid))
                    continue

                data = dict(_id=keyid, in_time=util.get_now_time(), province=self.province)
                data['url'] = url
                data['text'] = r.text

                # 发送数据到消息队列
                self.mq_queue_thread.push(data)
                break
            except Exception as e:
                self.log.exception(e)
        return data

    def query_company(self, i_d):
        result = self.CRAWL_UN_FINISH
        try:
            session = self.get_new_session(proxy_type=2)
            # 解析得到列表详情
            result = self.save_keyid_detail(session, i_d)
            if result > 0:
                result = self.CRAWL_FINISH
            elif result < 0:
                result = self.CRAWL_NOTHING_FIND
            del session
        except Exception as e:
            self.log.exception(e)
        return result

    def query_task(self, item):

        if not isinstance(item, dict):
            self.log.info('参数错误: item = {item}'.format(item=item))
            return self.FAIL

        # 判断公司信息字段有没有问题
        i_d = item.get('_id', '')
        if i_d == '':
            self.log.info('参数错误: 没有company_name字段..item = {item}'.format(item=item))
            return self.FAIL

        # 判断是否已经抓取过了
        if self.target_db.find_one(self.target_table, {'_id': i_d}) is not None:
            # 防止下次再把数据取出
            self.set_crawl_flag(item, self.crawl_flag, self.CRAWL_FINISH)
            return self.SUCCESS

        self.log.info('开始抓取任务...id = {id}'.format(id=i_d))
        status = self.query_company(i_d)
        self.set_crawl_flag(item, self.crawl_flag, status)
        self.log.info('完成抓取任务...id = {id} status = {status}'.format(id=i_d, status=status))
        if status == self.CRAWL_FINISH:
            return self.SUCCESS
        return self.FAIL
